home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevm1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  22.1 KB  |  746 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevm1.c,v 1.2 2000/09/19 19:00:13 lpd Exp $ */
  20. /* Monobit "memory" (stored bitmap) device */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gxdevice.h"
  24. #include "gxdevmem.h"        /* semi-public definitions */
  25. #include "gdevmem.h"        /* private definitions */
  26.  
  27. /* Optionally, use the slow RasterOp implementations for testing. */
  28. /*#define USE_COPY_ROP */
  29.  
  30. #ifdef USE_COPY_ROP
  31. #include "gsrop.h"
  32. #endif
  33.  
  34. /* ================ Standard (byte-oriented) device ================ */
  35.  
  36. /* Procedures */
  37. private dev_proc_map_rgb_color(mem_mono_map_rgb_color);
  38. private dev_proc_map_color_rgb(mem_mono_map_color_rgb);
  39. private dev_proc_copy_mono(mem_mono_copy_mono);
  40. private dev_proc_fill_rectangle(mem_mono_fill_rectangle);
  41. private dev_proc_strip_tile_rectangle(mem_mono_strip_tile_rectangle);
  42.  
  43. /* The device descriptor. */
  44. /* The instance is public. */
  45. const gx_device_memory mem_mono_device =
  46. mem_full_alpha_device("image1", 0, 1, mem_open,
  47.               mem_mono_map_rgb_color, mem_mono_map_color_rgb,
  48.      mem_mono_copy_mono, gx_default_copy_color, mem_mono_fill_rectangle,
  49.               gx_default_map_cmyk_color, gx_no_copy_alpha,
  50.               mem_mono_strip_tile_rectangle, mem_mono_strip_copy_rop,
  51.               mem_get_bits_rectangle);
  52.  
  53. /* Map color to/from RGB.  This may be inverted. */
  54. private gx_color_index
  55. mem_mono_map_rgb_color(gx_device * dev, gx_color_value r, gx_color_value g,
  56.                gx_color_value b)
  57. {
  58.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  59.  
  60.     return (gx_default_w_b_map_rgb_color(dev, r, g, b) ^
  61.         mdev->palette.data[0]) & 1;
  62. }
  63. private int
  64. mem_mono_map_color_rgb(gx_device * dev, gx_color_index color,
  65.                gx_color_value prgb[3])
  66. {
  67.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  68.  
  69.     return gx_default_w_b_map_color_rgb(dev,
  70.                     (color ^ mdev->palette.data[0]) & 1,
  71.                     prgb);
  72. }
  73.  
  74. /* Fill a rectangle with a color. */
  75. private int
  76. mem_mono_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  77.             gx_color_index color)
  78. {
  79.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  80.  
  81. #ifdef USE_COPY_ROP
  82.     return mem_mono_copy_rop(dev, NULL, 0, 0, gx_no_bitmap_id, NULL,
  83.                  NULL, NULL,
  84.                  x, y, w, h, 0, 0,
  85.                  (color ? rop3_1 : rop3_0));
  86. #else
  87.     fit_fill(dev, x, y, w, h);
  88.     bits_fill_rectangle(scan_line_base(mdev, y), x, mdev->raster,
  89.             -(mono_fill_chunk) color, w, h);
  90.     return 0;
  91. #endif
  92. }
  93.  
  94. /* Convert x coordinate to byte offset in scan line. */
  95. #define x_to_byte(x) ((x) >> 3)
  96.  
  97. /* Copy a monochrome bitmap. */
  98. #undef mono_masks
  99. #define mono_masks mono_copy_masks
  100.  
  101. /*
  102.  * Fetch a chunk from the source.
  103.  *
  104.  * Since source and destination are both always big-endian,
  105.  * fetching an aligned chunk never requires byte swapping.
  106.  */
  107. #define CFETCH_ALIGNED(cptr)\
  108.   (*(const chunk *)(cptr))
  109.  
  110. /*
  111.  * Note that the macros always cast cptr,
  112.  * so it doesn't matter what the type of cptr is.
  113.  */
  114. /* cshift = chunk_bits - shift. */
  115. #undef chunk
  116. #if arch_is_big_endian
  117. #  define chunk uint
  118. #  define CFETCH_RIGHT(cptr, shift, cshift)\
  119.     (CFETCH_ALIGNED(cptr) >> shift)
  120. #  define CFETCH_LEFT(cptr, shift, cshift)\
  121.     (CFETCH_ALIGNED(cptr) << shift)
  122. #  define CFETCH_USES_CSKEW 0
  123. /* Fetch a chunk that straddles a chunk boundary. */
  124. #  define CFETCH2(cptr, cskew, skew)\
  125.     (CFETCH_LEFT(cptr, cskew, skew) +\
  126.      CFETCH_RIGHT((const chunk *)(cptr) + 1, skew, cskew))
  127. #else /* little-endian */
  128. #  define chunk bits16
  129. private const bits16 right_masks2[9] =
  130. {
  131.     0xffff, 0x7f7f, 0x3f3f, 0x1f1f, 0x0f0f, 0x0707, 0x0303, 0x0101, 0x0000
  132. };
  133. private const bits16 left_masks2[9] =
  134. {
  135.     0xffff, 0xfefe, 0xfcfc, 0xf8f8, 0xf0f0, 0xe0e0, 0xc0c0, 0x8080, 0x0000
  136. };
  137.  
  138. #  define CCONT(cptr, off) (((const chunk *)(cptr))[off])
  139. #  define CFETCH_RIGHT(cptr, shift, cshift)\
  140.     ((shift) < 8 ?\
  141.      ((CCONT(cptr, 0) >> (shift)) & right_masks2[shift]) +\
  142.       (CCONT(cptr, 0) << (cshift)) :\
  143.      ((chunk)*(const byte *)(cptr) << (cshift)) & 0xff00)
  144. #  define CFETCH_LEFT(cptr, shift, cshift)\
  145.     ((shift) < 8 ?\
  146.      ((CCONT(cptr, 0) << (shift)) & left_masks2[shift]) +\
  147.       (CCONT(cptr, 0) >> (cshift)) :\
  148.      ((CCONT(cptr, 0) & 0xff00) >> (cshift)) & 0xff)
  149. #  define CFETCH_USES_CSKEW 1
  150. /* Fetch a chunk that straddles a chunk boundary. */
  151. /* We can avoid testing the shift amount twice */
  152. /* by expanding the CFETCH_LEFT/right macros in-line. */
  153. #  define CFETCH2(cptr, cskew, skew)\
  154.     ((cskew) < 8 ?\
  155.      ((CCONT(cptr, 0) << (cskew)) & left_masks2[cskew]) +\
  156.       (CCONT(cptr, 0) >> (skew)) +\
  157.       (((chunk)(((const byte *)(cptr))[2]) << (cskew)) & 0xff00) :\
  158.      (((CCONT(cptr, 0) & 0xff00) >> (skew)) & 0xff) +\
  159.       ((CCONT(cptr, 1) >> (skew)) & right_masks2[skew]) +\
  160.        (CCONT(cptr, 1) << (cskew)))
  161. #endif
  162.  
  163. typedef enum {
  164.     COPY_OR = 0, COPY_STORE, COPY_AND, COPY_FUNNY
  165. } copy_function;
  166. typedef struct {
  167.     int invert;
  168.     copy_function op;
  169. } copy_mode;
  170.  
  171. /*
  172.  * Map from <color0,color1> to copy_mode.
  173.  * Logically, this is a 2-D array.
  174.  * The indexing is (transparent, 0, 1, unused). */
  175. private const copy_mode copy_modes[16] = {
  176.     {~0, COPY_FUNNY},        /* NN */
  177.     {~0, COPY_AND},        /* N0 */
  178.     {0, COPY_OR},        /* N1 */
  179.     {0, 0},            /* unused */
  180.     {0, COPY_AND},        /* 0N */
  181.     {0, COPY_FUNNY},        /* 00 */
  182.     {0, COPY_STORE},        /* 01 */
  183.     {0, 0},            /* unused */
  184.     {~0, COPY_OR},        /* 1N */
  185.     {~0, COPY_STORE},        /* 10 */
  186.     {0, COPY_FUNNY},        /* 11 */
  187.     {0, 0},            /* unused */
  188.     {0, 0},            /* unused */
  189.     {0, 0},            /* unused */
  190.     {0, 0},            /* unused */
  191.     {0, 0},            /* unused */
  192. };
  193.  
  194. /* Handle the funny cases that aren't supposed to happen. */
  195. #define FUNNY_CASE()\
  196.   (invert ? gs_note_error(-1) :\
  197.    mem_mono_fill_rectangle(dev, x, y, w, h, color0))
  198.  
  199. private int
  200. mem_mono_copy_mono(gx_device * dev,
  201.  const byte * source_data, int source_x, int source_raster, gx_bitmap_id id,
  202.    int x, int y, int w, int h, gx_color_index color0, gx_color_index color1)
  203. {
  204.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  205.  
  206. #ifdef USE_COPY_ROP
  207.     return mem_mono_copy_rop(dev, source_data, source_x, source_raster,
  208.                  id, NULL, NULL, NULL,
  209.                  x, y, w, h, 0, 0,
  210.                  ((color0 == gx_no_color_index ? rop3_D :
  211.                    color0 == 0 ? rop3_0 : rop3_1) & ~rop3_S) |
  212.                  ((color1 == gx_no_color_index ? rop3_D :
  213.                    color1 == 0 ? rop3_0 : rop3_1) & rop3_S));
  214. #else /* !USE_COPY_ROP */
  215.     register const byte *bptr;    /* actually chunk * */
  216.     int dbit, wleft;
  217.     uint mask;
  218.     copy_mode mode;
  219.  
  220.     DECLARE_SCAN_PTR_VARS(dbptr, byte *, dest_raster);
  221. #define optr ((chunk *)dbptr)
  222.     register int skew;
  223.     register uint invert;
  224.  
  225.     fit_copy(dev, source_data, source_x, source_raster, id, x, y, w, h);
  226. #if gx_no_color_index_value != -1    /* hokey! */
  227.     if (color0 == gx_no_color_index)
  228.     color0 = -1;
  229.     if (color1 == gx_no_color_index)
  230.     color1 = -1;
  231. #endif
  232.     mode = copy_modes[((int)color0 << 2) + (int)color1 + 5];
  233.     invert = (uint)mode.invert;    /* load register */
  234.     SETUP_RECT_VARS(dbptr, byte *, dest_raster);
  235.     bptr = source_data + ((source_x & ~chunk_align_bit_mask) >> 3);
  236.     dbit = x & chunk_align_bit_mask;
  237.     skew = dbit - (source_x & chunk_align_bit_mask);
  238.  
  239. /* Macros for writing partial chunks. */
  240. /* The destination pointer is always named optr, */
  241. /* and must be declared as chunk *. */
  242. /* CINVERT may be temporarily redefined. */
  243. #define CINVERT(bits) ((bits) ^ invert)
  244. #define WRITE_OR_MASKED(bits, mask, off)\
  245.   optr[off] |= (CINVERT(bits) & mask)
  246. #define WRITE_STORE_MASKED(bits, mask, off)\
  247.   optr[off] = ((optr[off] & ~mask) | (CINVERT(bits) & mask))
  248. #define WRITE_AND_MASKED(bits, mask, off)\
  249.   optr[off] &= (CINVERT(bits) | ~mask)
  250. /* Macros for writing full chunks. */
  251. #define WRITE_OR(bits)  *optr |= CINVERT(bits)
  252. #define WRITE_STORE(bits) *optr = CINVERT(bits)
  253. #define WRITE_AND(bits) *optr &= CINVERT(bits)
  254. /* Macro for incrementing to next chunk. */
  255. #define NEXT_X_CHUNK()\
  256.   bptr += chunk_bytes; dbptr += chunk_bytes
  257. /* Common macro for the end of each scan line. */
  258. #define END_Y_LOOP(sdelta, ddelta)\
  259.   bptr += sdelta; dbptr += ddelta
  260.  
  261.     if ((wleft = w + dbit - chunk_bits) <= 0) {        /* The entire operation fits in one (destination) chunk. */
  262.     set_mono_thin_mask(mask, w, dbit);
  263.  
  264. #define WRITE_SINGLE(wr_op, src)\
  265.   for ( ; ; )\
  266.    { wr_op(src, mask, 0);\
  267.      if ( --h == 0 ) break;\
  268.      END_Y_LOOP(source_raster, dest_raster);\
  269.    }
  270.  
  271. #define WRITE1_LOOP(src)\
  272.   switch ( mode.op ) {\
  273.     case COPY_OR: WRITE_SINGLE(WRITE_OR_MASKED, src); break;\
  274.     case COPY_STORE: WRITE_SINGLE(WRITE_STORE_MASKED, src); break;\
  275.     case COPY_AND: WRITE_SINGLE(WRITE_AND_MASKED, src); break;\
  276.     default: return FUNNY_CASE();\
  277.   }
  278.  
  279.     if (skew >= 0) {    /* single -> single, right/no shift */
  280.         if (skew == 0) {    /* no shift */
  281.         WRITE1_LOOP(CFETCH_ALIGNED(bptr));
  282.         } else {        /* right shift */
  283. #if CFETCH_USES_CSKEW
  284.         int cskew = chunk_bits - skew;
  285. #endif
  286.  
  287.         WRITE1_LOOP(CFETCH_RIGHT(bptr, skew, cskew));
  288.         }
  289.     } else if (wleft <= skew) {    /* single -> single, left shift */
  290. #if CFETCH_USES_CSKEW
  291.         int cskew = chunk_bits + skew;
  292. #endif
  293.  
  294.         skew = -skew;
  295.         WRITE1_LOOP(CFETCH_LEFT(bptr, skew, cskew));
  296.     } else {        /* double -> single */
  297.         int cskew = -skew;
  298.  
  299.         skew += chunk_bits;
  300.         WRITE1_LOOP(CFETCH2(bptr, cskew, skew));
  301.     }
  302. #undef WRITE1_LOOP
  303. #undef WRITE_SINGLE
  304.     } else if (wleft <= skew) {    /* 1 source chunk -> 2 destination chunks. */
  305.     /* This is an important special case for */
  306.     /* both characters and halftone tiles. */
  307.     uint rmask;
  308.     int cskew = chunk_bits - skew;
  309.  
  310.     set_mono_left_mask(mask, dbit);
  311.     set_mono_right_mask(rmask, wleft);
  312. #undef CINVERT
  313. #define CINVERT(bits) (bits)    /* pre-inverted here */
  314.  
  315. #if arch_is_big_endian        /* no byte swapping */
  316. #  define WRITE_1TO2(wr_op)\
  317.   for ( ; ; )\
  318.    { register uint bits = CFETCH_ALIGNED(bptr) ^ invert;\
  319.      wr_op(bits >> skew, mask, 0);\
  320.      wr_op(bits << cskew, rmask, 1);\
  321.      if ( --h == 0 ) break;\
  322.      END_Y_LOOP(source_raster, dest_raster);\
  323.    }
  324. #else /* byte swapping */
  325. #  define WRITE_1TO2(wr_op)\
  326.   for ( ; ; )\
  327.    { wr_op(CFETCH_RIGHT(bptr, skew, cskew) ^ invert, mask, 0);\
  328.      wr_op(CFETCH_LEFT(bptr, cskew, skew) ^ invert, rmask, 1);\
  329.      if ( --h == 0 ) break;\
  330.      END_Y_LOOP(source_raster, dest_raster);\
  331.    }
  332. #endif
  333.  
  334.     switch (mode.op) {
  335.         case COPY_OR:
  336.         WRITE_1TO2(WRITE_OR_MASKED);
  337.         break;
  338.         case COPY_STORE:
  339.         WRITE_1TO2(WRITE_STORE_MASKED);
  340.         break;
  341.         case COPY_AND:
  342.         WRITE_1TO2(WRITE_AND_MASKED);
  343.         break;
  344.         default:
  345.         return FUNNY_CASE();
  346.     }
  347. #undef CINVERT
  348. #define CINVERT(bits) ((bits) ^ invert)
  349. #undef WRITE_1TO2
  350.     } else {            /* More than one source chunk and more than one */
  351.     /* destination chunk are involved. */
  352.     uint rmask;
  353.     int words = (wleft & ~chunk_bit_mask) >> 3;
  354.     uint sskip = source_raster - words;
  355.     uint dskip = dest_raster - words;
  356.     register uint bits;
  357.  
  358.     set_mono_left_mask(mask, dbit);
  359.     set_mono_right_mask(rmask, wleft & chunk_bit_mask);
  360.     if (skew == 0) {    /* optimize the aligned case */
  361.  
  362. #define WRITE_ALIGNED(wr_op, wr_op_masked)\
  363.   for ( ; ; )\
  364.    { int count = wleft;\
  365.      /* Do first partial chunk. */\
  366.      wr_op_masked(CFETCH_ALIGNED(bptr), mask, 0);\
  367.      /* Do full chunks. */\
  368.      while ( (count -= chunk_bits) >= 0 )\
  369.       { NEXT_X_CHUNK(); wr_op(CFETCH_ALIGNED(bptr)); }\
  370.      /* Do last chunk */\
  371.      if ( count > -chunk_bits )\
  372.       { wr_op_masked(CFETCH_ALIGNED(bptr + chunk_bytes), rmask, 1); }\
  373.      if ( --h == 0 ) break;\
  374.      END_Y_LOOP(sskip, dskip);\
  375.    }
  376.  
  377.         switch (mode.op) {
  378.         case COPY_OR:
  379.             WRITE_ALIGNED(WRITE_OR, WRITE_OR_MASKED);
  380.             break;
  381.         case COPY_STORE:
  382.             WRITE_ALIGNED(WRITE_STORE, WRITE_STORE_MASKED);
  383.             break;
  384.         case COPY_AND:
  385.             WRITE_ALIGNED(WRITE_AND, WRITE_AND_MASKED);
  386.             break;
  387.         default:
  388.             return FUNNY_CASE();
  389.         }
  390. #undef WRITE_ALIGNED
  391.     } else {        /* not aligned */
  392.         int cskew = -skew & chunk_bit_mask;
  393.         bool case_right =
  394.         (skew >= 0 ? true :
  395.          ((bptr += chunk_bytes), false));
  396.  
  397.         skew &= chunk_bit_mask;
  398.  
  399. #define WRITE_UNALIGNED(wr_op, wr_op_masked)\
  400.   /* Prefetch partial word. */\
  401.   bits =\
  402.     (case_right ? CFETCH_RIGHT(bptr, skew, cskew) :\
  403.      CFETCH2(bptr - chunk_bytes, cskew, skew));\
  404.   wr_op_masked(bits, mask, 0);\
  405.   /* Do full chunks. */\
  406.   while ( count >= chunk_bits )\
  407.     { bits = CFETCH2(bptr, cskew, skew);\
  408.       NEXT_X_CHUNK(); wr_op(bits); count -= chunk_bits;\
  409.     }\
  410.   /* Do last chunk */\
  411.   if ( count > 0 )\
  412.     { bits = CFETCH_LEFT(bptr, cskew, skew);\
  413.       if ( count > skew ) bits += CFETCH_RIGHT(bptr + chunk_bytes, skew, cskew);\
  414.       wr_op_masked(bits, rmask, 1);\
  415.     }
  416.  
  417.         switch (mode.op) {
  418.         case COPY_OR:
  419.             for (;;) {
  420.             int count = wleft;
  421.  
  422.             WRITE_UNALIGNED(WRITE_OR, WRITE_OR_MASKED);
  423.             if (--h == 0)
  424.                 break;
  425.             END_Y_LOOP(sskip, dskip);
  426.             }
  427.             break;
  428.         case COPY_STORE:
  429.             for (;;) {
  430.             int count = wleft;
  431.  
  432.             WRITE_UNALIGNED(WRITE_STORE, WRITE_STORE_MASKED);
  433.             if (--h == 0)
  434.                 break;
  435.             END_Y_LOOP(sskip, dskip);
  436.             }
  437.             break;
  438.         case COPY_AND:
  439.             for (;;) {
  440.             int count = wleft;
  441.  
  442.             WRITE_UNALIGNED(WRITE_AND, WRITE_AND_MASKED);
  443.             if (--h == 0)
  444.                 break;
  445.             END_Y_LOOP(sskip, dskip);
  446.             }
  447.             break;
  448.         default /*case COPY_FUNNY */ :
  449.             return FUNNY_CASE();
  450.         }
  451. #undef WRITE_UNALIGNED
  452.     }
  453.     }
  454. #undef END_Y_LOOP
  455. #undef NEXT_X_CHUNK
  456.     return 0;
  457. #undef optr
  458. #endif /* !USE_COPY_ROP */
  459. }
  460.  
  461. /* Strip-tile with a monochrome halftone. */
  462. /* This is a performance bottleneck for monochrome devices, */
  463. /* so we re-implement it, even though it takes a lot of code. */
  464. private int
  465. mem_mono_strip_tile_rectangle(gx_device * dev,
  466.                   register const gx_strip_bitmap * tiles,
  467. int tx, int y, int tw, int th, gx_color_index color0, gx_color_index color1,
  468.                   int px, int py)
  469. {
  470.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  471.  
  472. #ifdef USE_COPY_ROP
  473.     return mem_mono_strip_copy_rop(dev, NULL, 0, 0, tile->id, NULL,
  474.                    tiles, NULL,
  475.                    tx, y, tw, th, px, py,
  476.                    ((color0 == gx_no_color_index ? rop3_D :
  477.                  color0 == 0 ? rop3_0 : rop3_1) & ~rop3_T) |
  478.                    ((color1 == gx_no_color_index ? rop3_D :
  479.                   color1 == 0 ? rop3_0 : rop3_1) & rop3_T));
  480. #else /* !USE_COPY_ROP */
  481.     register uint invert;
  482.     int source_raster;
  483.     uint tile_bits_size;
  484.     const byte *source_data;
  485.     const byte *end;
  486.     int x, rw, w, h;
  487.     register const byte *bptr;    /* actually chunk * */
  488.     int dbit, wleft;
  489.     uint mask;
  490.     byte *dbase;
  491.  
  492.     DECLARE_SCAN_PTR_VARS(dbptr, byte *, dest_raster);
  493. #define optr ((chunk *)dbptr)
  494.     register int skew;
  495.  
  496.     /* This implementation doesn't handle strips yet. */
  497.     if (color0 != (color1 ^ 1) || tiles->shift != 0)
  498.     return gx_default_strip_tile_rectangle(dev, tiles, tx, y, tw, th,
  499.                            color0, color1, px, py);
  500.     fit_fill(dev, tx, y, tw, th);
  501.     invert = -(uint) color0;
  502.     source_raster = tiles->raster;
  503.     source_data = tiles->data + ((y + py) % tiles->rep_height) * source_raster;
  504.     tile_bits_size = tiles->size.y * source_raster;
  505.     end = tiles->data + tile_bits_size;
  506. #undef END_Y_LOOP
  507. #define END_Y_LOOP(sdelta, ddelta)\
  508.   if ( end - bptr <= sdelta )    /* wrap around */\
  509.     bptr -= tile_bits_size;\
  510.   bptr += sdelta; dbptr += ddelta
  511.     dest_raster = mdev->raster;
  512.     dbase = scan_line_base(mdev, y);
  513.     x = tx;
  514.     rw = tw;
  515.     /*
  516.      * The outermost loop here works horizontally, one iteration per
  517.      * copy of the tile.  Note that all iterations except the first
  518.      * have source_x = 0.
  519.      */
  520.     {
  521.     int source_x = (x + px) % tiles->rep_width;
  522.  
  523.     w = tiles->size.x - source_x;
  524.     bptr = source_data + ((source_x & ~chunk_align_bit_mask) >> 3);
  525.     dbit = x & chunk_align_bit_mask;
  526.     skew = dbit - (source_x & chunk_align_bit_mask);
  527.     }
  528.   outer:if (w > rw)
  529.     w = rw;
  530.     h = th;
  531.     dbptr = dbase + ((x >> 3) & -chunk_align_bytes);
  532.     if ((wleft = w + dbit - chunk_bits) <= 0) {        /* The entire operation fits in one (destination) chunk. */
  533.     set_mono_thin_mask(mask, w, dbit);
  534. #define WRITE1_LOOP(src)\
  535.   for ( ; ; )\
  536.    { WRITE_STORE_MASKED(src, mask, 0);\
  537.      if ( --h == 0 ) break;\
  538.      END_Y_LOOP(source_raster, dest_raster);\
  539.    }
  540.     if (skew >= 0) {    /* single -> single, right/no shift */
  541.         if (skew == 0) {    /* no shift */
  542.         WRITE1_LOOP(CFETCH_ALIGNED(bptr));
  543.         } else {        /* right shift */
  544. #if CFETCH_USES_CSKEW
  545.         int cskew = chunk_bits - skew;
  546. #endif
  547.  
  548.         WRITE1_LOOP(CFETCH_RIGHT(bptr, skew, cskew));
  549.         }
  550.     } else if (wleft <= skew) {    /* single -> single, left shift */
  551. #if CFETCH_USES_CSKEW
  552.         int cskew = chunk_bits + skew;
  553. #endif
  554.  
  555.         skew = -skew;
  556.         WRITE1_LOOP(CFETCH_LEFT(bptr, skew, cskew));
  557.     } else {        /* double -> single */
  558.         int cskew = -skew;
  559.  
  560.         skew += chunk_bits;
  561.         WRITE1_LOOP(CFETCH2(bptr, cskew, skew));
  562.     }
  563. #undef WRITE1_LOOP
  564.     } else if (wleft <= skew) {    /* 1 source chunk -> 2 destination chunks. */
  565.     /* This is an important special case for */
  566.     /* both characters and halftone tiles. */
  567.     uint rmask;
  568.     int cskew = chunk_bits - skew;
  569.  
  570.     set_mono_left_mask(mask, dbit);
  571.     set_mono_right_mask(rmask, wleft);
  572. #if arch_is_big_endian        /* no byte swapping */
  573. #undef CINVERT
  574. #define CINVERT(bits) (bits)    /* pre-inverted here */
  575.     for (;;) {
  576.         register uint bits = CFETCH_ALIGNED(bptr) ^ invert;
  577.  
  578.         WRITE_STORE_MASKED(bits >> skew, mask, 0);
  579.         WRITE_STORE_MASKED(bits << cskew, rmask, 1);
  580.         if (--h == 0)
  581.         break;
  582.         END_Y_LOOP(source_raster, dest_raster);
  583.     }
  584. #undef CINVERT
  585. #define CINVERT(bits) ((bits) ^ invert)
  586. #else /* byte swapping */
  587.     for (;;) {
  588.         WRITE_STORE_MASKED(CFETCH_RIGHT(bptr, skew, cskew), mask, 0);
  589.         WRITE_STORE_MASKED(CFETCH_LEFT(bptr, cskew, skew), rmask, 1);
  590.         if (--h == 0)
  591.         break;
  592.         END_Y_LOOP(source_raster, dest_raster);
  593.     }
  594. #endif
  595.     } else {            /* More than one source chunk and more than one */
  596.     /* destination chunk are involved. */
  597.     uint rmask;
  598.     int words = (wleft & ~chunk_bit_mask) >> 3;
  599.     uint sskip = source_raster - words;
  600.     uint dskip = dest_raster - words;
  601.     register uint bits;
  602.  
  603. #define NEXT_X_CHUNK()\
  604.   bptr += chunk_bytes; dbptr += chunk_bytes
  605.  
  606.     set_mono_right_mask(rmask, wleft & chunk_bit_mask);
  607.     if (skew == 0) {    /* optimize the aligned case */
  608.         if (dbit == 0)
  609.         mask = 0;
  610.         else
  611.         set_mono_left_mask(mask, dbit);
  612.         for (;;) {
  613.         int count = wleft;
  614.  
  615.         /* Do first partial chunk. */
  616.         if (mask)
  617.             WRITE_STORE_MASKED(CFETCH_ALIGNED(bptr), mask, 0);
  618.         else
  619.             WRITE_STORE(CFETCH_ALIGNED(bptr));
  620.         /* Do full chunks. */
  621.         while ((count -= chunk_bits) >= 0) {
  622.             NEXT_X_CHUNK();
  623.             WRITE_STORE(CFETCH_ALIGNED(bptr));
  624.         }
  625.         /* Do last chunk */
  626.         if (count > -chunk_bits) {
  627.             WRITE_STORE_MASKED(CFETCH_ALIGNED(bptr + chunk_bytes), rmask, 1);
  628.         }
  629.         if (--h == 0)
  630.             break;
  631.         END_Y_LOOP(sskip, dskip);
  632.         }
  633.     } else {        /* not aligned */
  634.         bool case_right =
  635.         (skew >= 0 ? true :
  636.          ((bptr += chunk_bytes), false));
  637.         int cskew = -skew & chunk_bit_mask;
  638.  
  639.         skew &= chunk_bit_mask;
  640.         set_mono_left_mask(mask, dbit);
  641.         for (;;) {
  642.         int count = wleft;
  643.  
  644.         if (case_right)
  645.             bits = CFETCH_RIGHT(bptr, skew, cskew);
  646.         else
  647.             bits = CFETCH2(bptr - chunk_bytes, cskew, skew);
  648.         WRITE_STORE_MASKED(bits, mask, 0);
  649.         /* Do full chunks. */
  650.         while (count >= chunk_bits) {
  651.             bits = CFETCH2(bptr, cskew, skew);
  652.             NEXT_X_CHUNK();
  653.             WRITE_STORE(bits);
  654.             count -= chunk_bits;
  655.         }
  656.         /* Do last chunk */
  657.         if (count > 0) {
  658.             bits = CFETCH_LEFT(bptr, cskew, skew);
  659.             if (count > skew)
  660.             bits += CFETCH_RIGHT(bptr + chunk_bytes, skew, cskew);
  661.             WRITE_STORE_MASKED(bits, rmask, 1);
  662.         }
  663.         if (--h == 0)
  664.             break;
  665.         END_Y_LOOP(sskip, dskip);
  666.         }
  667.     }
  668.     }
  669. #undef END_Y_LOOP
  670. #undef NEXT_X_CHUNK
  671. #undef optr
  672.     if ((rw -= w) > 0) {
  673.     x += w;
  674.     w = tiles->size.x;
  675.     bptr = source_data;
  676.     skew = dbit = x & chunk_align_bit_mask;
  677.     goto outer;
  678.     }
  679.     return 0;
  680. #endif /* !USE_COPY_ROP */
  681. }
  682.  
  683. /* ================ "Word"-oriented device ================ */
  684.  
  685. /* Note that on a big-endian machine, this is the same as the */
  686. /* standard byte-oriented-device. */
  687.  
  688. #if !arch_is_big_endian
  689.  
  690. /* Procedures */
  691. private dev_proc_copy_mono(mem1_word_copy_mono);
  692. private dev_proc_fill_rectangle(mem1_word_fill_rectangle);
  693.  
  694. #define mem1_word_strip_tile_rectangle gx_default_strip_tile_rectangle
  695.  
  696. /* Here is the device descriptor. */
  697. const gx_device_memory mem_mono_word_device =
  698. mem_full_alpha_device("image1w", 0, 1, mem_open,
  699.               mem_mono_map_rgb_color, mem_mono_map_color_rgb,
  700.        mem1_word_copy_mono, gx_default_copy_color, mem1_word_fill_rectangle,
  701.               gx_default_map_cmyk_color, gx_no_copy_alpha,
  702.               mem1_word_strip_tile_rectangle, gx_no_strip_copy_rop,
  703.               mem_word_get_bits_rectangle);
  704.  
  705. /* Fill a rectangle with a color. */
  706. private int
  707. mem1_word_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  708.              gx_color_index color)
  709. {
  710.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  711.     byte *base;
  712.     uint raster;
  713.  
  714.     fit_fill(dev, x, y, w, h);
  715.     base = scan_line_base(mdev, y);
  716.     raster = mdev->raster;
  717.     mem_swap_byte_rect(base, raster, x, w, h, true);
  718.     bits_fill_rectangle(base, x, raster, -(mono_fill_chunk) color, w, h);
  719.     mem_swap_byte_rect(base, raster, x, w, h, true);
  720.     return 0;
  721. }
  722.  
  723. /* Copy a bitmap. */
  724. private int
  725. mem1_word_copy_mono(gx_device * dev,
  726.  const byte * source_data, int source_x, int source_raster, gx_bitmap_id id,
  727.    int x, int y, int w, int h, gx_color_index color0, gx_color_index color1)
  728. {
  729.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  730.     byte *row;
  731.     uint raster;
  732.     bool store;
  733.  
  734.     fit_copy(dev, source_data, source_x, source_raster, id, x, y, w, h);
  735.     row = scan_line_base(mdev, y);
  736.     raster = mdev->raster;
  737.     store = (color0 != gx_no_color_index && color1 != gx_no_color_index);
  738.     mem_swap_byte_rect(row, raster, x, w, h, store);
  739.     mem_mono_copy_mono(dev, source_data, source_x, source_raster, id,
  740.                x, y, w, h, color0, color1);
  741.     mem_swap_byte_rect(row, raster, x, w, h, false);
  742.     return 0;
  743. }
  744.  
  745. #endif /* !arch_is_big_endian */
  746.